/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
package de.rockon.fuzzy.controller.gui.propertyeditor;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import de.rockon.fuzzy.controller.model.FuzzyBasicElement;
import de.rockon.fuzzy.controller.model.FuzzyPoint;
import de.rockon.fuzzy.controller.util.factories.UIFactory;
import de.rockon.fuzzy.exceptions.DuplicateXValueException;
import de.rockon.fuzzy.exceptions.ValueOutOfDomainException;

public class PropertyPoint extends PropertyBase {
	private static final long serialVersionUID = -4895222776331788352L;
	private JLabel lblX;
	private JTextField txtX;
	private JLabel lblY;
	private JTextField txtY;
	private FuzzyPoint property;

	public PropertyPoint() {
		initGUI();
	}

	@Override
	public void getPropertyValues(FuzzyBasicElement<?, ?> property) {
		this.property = (FuzzyPoint) property;

		resetProperties();
	}

	@Override
	public void initGUI() {
		FormLayout layout = new FormLayout("1px, right:pref, 1dlu, pref:grow, 1px", "1px, pref, 1dlu, pref, 1dlu, pref, 1px");

		PanelBuilder builder = new PanelBuilder(layout);
		CellConstraints cc = new CellConstraints();

		lblX = UIFactory.getJLabel("X: ", null, SwingConstants.RIGHT);
		txtX = UIFactory.getJTextField("0.00", 20, null, this);

		lblY = UIFactory.getJLabel("Y: ", null, SwingConstants.RIGHT);
		txtY = UIFactory.getJTextField("0.00", 20, null, this);

		// setze alle Tooltips
		String toolTipX = "The name of the new variable";
		String toolTipY = "sets the Color of the current Set";

		lblX.setToolTipText(toolTipX);
		txtX.setToolTipText(toolTipX);
		lblY.setToolTipText(toolTipY);
		txtY.setToolTipText(toolTipY);

		builder.addSeparator("Point Properties", cc.xyw(1, 2, 5));
		builder.add(lblX, cc.xy(2, 4));
		builder.add(txtX, cc.xy(4, 4));

		builder.add(lblY, cc.xy(2, 6));
		builder.add(txtY, cc.xy(4, 6));

		add(builder.getPanel());
	}

	@Override
	public void resetProperties() {
		if (property != null) {
			txtX.setText("" + property.getX());
			txtY.setText("" + property.getY());
		}
	}

	@Override
	public void saveProperties() {
		try {
			property.setX(Double.parseDouble(txtX.getText()));
			property.setY(Double.parseDouble(txtY.getText()));
		} catch (NumberFormatException e) {
			UIFactory.showErrorDialog(null, e.getMessage());
		} catch (ValueOutOfDomainException e2) {
			UIFactory.showErrorDialog(null, e2.getMessage());
		} catch (DuplicateXValueException e3) {
			UIFactory.showErrorDialog(null, e3.getMessage());
		}
	}
}
